{include file="header.html"}
<style type="text/css">
	.content img{ width: 100% !important;height: auto !important;}
</style>
<body class="gr">

<div class="navBar navBar-hidden">
	<a class="left" href="javascript:history.back()"><i class="return-back"></i></a>
	<div class="titleView">商品详情</div>
</div>

<div class="goods-detail main-bottom width-wrap">
	<div class="pics">
		{if count($data->pics)}
		<div class="slide">
			<ul>
				{foreach from=$data->pics name=p item=p}
				<li title="{$p->memo}"><div><a href="javascript:void(0)"><img src="{$p->pic}" /></a></div></li>
				{/foreach}
			</ul>
		</div>
		<div class="pager"></div>
		{else}
		<div class="pic" style="background-image:url({$data->default_pic});"></div>
		{/if}
	</div>
	<div class="detail">
		<div class="name">{$data->name}</div>
		{if !isset($integral)}
		<div class="price ge-bottom ge-light">
			<div><span>￥{number_format($data->price,2)}</span>　<s>原价￥{number_format($data->market_price,2)}</s></div>
		</div>
		<div class="country">
			<!--<span>{$data->sale_method_name}</span>-->
			<div></div>
			<font>{if $data->sale_method==1}此商品线下门店有售{/if}</font>
		</div>
		{else}
		<div class="integral">{$data->integral}积分</div>
		{/if}
	</div>
	
	<div class="param">
		<div class="ico1"></div>
		<span>人气 {$data->clicks}</span>
		<div class="ico2"></div>
		<span>销量 {$data->sales}</span>
		<div class="ico3"></div>
		<span class="label3">库存 {$data->stocks}</span>
	</div>
	
	<div class="detail-param">
		<ul>
			<li>
				<font>规格：</font>
				{foreach from=$data->specs item=g}
				<span><input type="radio" name="spec" id="spec{$g->id}" value="{$g->spec}" price="{$g->price}" /><label for="spec{$g->id}"><div>{$g->spec}</div></label></span>
				{/foreach}
				<div class="clear"></div>
			</li>
			{if !isset($integral)}
			<li>
				<font>数量：</font>
				<div class="num">
					<a href="javascript:void(0)" class="minus">-</a>
					<input type="tel" id="quantity" value="1" stocks="{$data->stocks}" />
					<a href="javascript:void(0)" class="plus">+</a>
				</div>
				<div class="clear"></div>
			</li>
			{/if}
		</ul>
	</div>
	
	{if !isset($integral)}
	<a href="wap.php?app=comment&act=index&goods_id={$data->id}&pagesize=10" class="comments push-ico">
		<font>查看评论</font><div>用户评价</div><span>({$data->comments})</span>
	</a>
	{/if}
	
	<div class="memo">
		<div class="tip">商品详情</div>
		<div class="content">
			{$data->content}
		</div>
	</div>
	
	{if !isset($integral)}
	<div class="bottomView toolBar-black ge-top">
		<a class="btn" href="javascript:void(0)">加入购物车</a>
		<!--<a class="im" href="javascript:void(0)" member_id="xxx"></a>-->
		<a class="fav {if $data->favorited==1}fav-x{/if}" href="javascript:void(0)"></a>
		<a class="cart badge" href="wap.php?app=cart&act=index"><sub></sub></a>
	</div>
	{else}
	<a class="integralBtn" href="javascript:void(0)">立即兑换</a>
	{/if}
</div>

<input type="hidden" id="goods_id" value="{$data->id}" />

{if isset($integral)}
<form action="wap.php?app=cart&act=jiesuan" method="post">
<input type="hidden" name="goods" id="goods" />
<input type="hidden" name="integral_order" value="1" />
</form>
{/if}

{include file="footer.html"}
<script>
function getCart(){
	$.getJSON('api.php?app=cart&act=cart_total', function(json){
		$('.badge sub').html(json.data.quantity>0?'<b>'+json.data.quantity+'</b>':'');
	});
}
function resize(){
	$('.goods-detail .pics, .goods-detail .pics .pic, .goods-detail .slide').autoHeight(640, 640);
	$('.goods-detail .slide').touchmove({
		title : 'title',
		drag : true,
		keydown : true,
		pager : '.pics .pager',
		offset : 'center',
		complete : function(){
			$('.pics li div').loadpic({ fill:this.height() });
		}
	});
}
$(window).resize(resize);
$(function(){
	resize();
	$(window).scroll(function(){
		if($(window).scrollTop()>=$('.pics').height()-$('.navBar').height()){
			$('.navBar').removeClass('navBar-hidden');
		}else{
			$('.navBar').addClass('navBar-hidden');
		}
	});
	
	{if !isset($integral)}
	getCart();
	$('.price span').priceFont('bigPrice');
	
	$(':radio:eq(0)').attr('checked', 'checked');
	$('.price span').html('￥'+$(':radio:checked').attr('price').numberFormat(2)).priceFont('bigPrice');
	
	$(':radio').change(function(){
		if(!$(this).is(':checked'))return;
		$('.price span').html('￥'+$(this).attr('price').numberFormat(2)).priceFont('bigPrice');
	});
	
	$('.minus').click(function(){
		var quantity = $('#quantity');
		if(quantity.val()<=1)return;
		quantity.val(quantity.val()*1-1);
	});
	$('.plus').click(function(){
		var quantity = $('#quantity');
		if(quantity.val()*1>quantity.attr('stocks')*1){
			$.overloadError('库存不足');
			return;
		}
		quantity.val(quantity.val()*1+1);
	});
	$('#quantity').focus(function(){
		var quantity = $(this);
		if(quantity.val().length)quantity.data('quantity', quantity.val());
	}).blur(function(){
		var quantity = $(this);
		if(!quantity.val().length){
			$.overloadError('请填写数量');
			quantity.focus();
			return;
		}
		if(quantity.val()*1>quantity.attr('stocks')*1){
			$.overloadError('库存不足');
			quantity.val(quantity.data('quantity'));
			quantity.focus();
			return;
		}
	});
	
	$('.bottomView .im').click(function(){
		var member_id = $(this).attr('member_id');
		location.href = '/wap.php?app=chat&act=talk';
		return false;
	});
	
	$('.bottomView .fav').click(function(){
		var _this = $(this);
		$.postJSON('api.php?app=favorite&act=add', { item_id:'{$data->id}', type_id:1 }, function(){
			if(_this.hasClass('fav-x'))_this.removeClass('fav-x');
			else _this.addClass('fav-x');
		});
	});
	
	$('.bottomView .btn').click(function(){
		if($(':radio').length && !$(':radio:checked').length){
			$.overloadError('请选择一个规格');
			return;
		}
		if(!$('#quantity').val().length){
			$.overloadError('请填写数量');
			$('#quantity').focus();
			return;
		}
		var _this = $(this), goods = [{ goods_id:$('#goods_id').val(), quantity:$('#quantity').val(), spec:$(':radio:checked').val() }];
		$.postJSON('api.php?app=cart&act=add_cart', { goods:$.jsonString(goods) }, function(json){
			var dot = $('<div class="badge-dot"></div>'), sub = $('.badge sub'), height = sub.height(), offset = sub.offset(), scrollTop = $(window).scrollTop();
			dot.parabola({
				start : {
					left : _this.offset().left + _this.width()/2,
					top : _this.offset().top + _this.height()/2 - scrollTop,
					width : height,
					height : height
				},
				end : {
					left : offset.left,
					top : offset.top - scrollTop,
				},
				after : function(){
					sub.html(json.data.quantity>0?'<b>'+json.data.quantity+'</b>':'');
					this.remove();
				}
			});
		});
	});
	
	{else}
	$(':radio:eq(0)').attr('checked', 'checked');
	$('.integral').html($(':radio:checked').attr('price').round(0)+'积分');
	
	$(':radio').change(function(){
		if(!$(this).is(':checked'))return;
		$('.integral').html($(this).attr('price').round(0)+'积分');
	});
	$('.integralBtn').click(function(){
		if(!$.checklogin())return;
		var goods = [{ goods_id:$('#goods_id').val(), quantity:1, spec:$(':radio:checked').val() }];
		$('#goods').val($.jsonString(goods));
		$('form').submit();
	});
	{/if}
});
</script>